﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Core;

namespace GSLMS.WebAPP.Areas.System.Controllers.SysParm
{
    public class SysParmController : AdminBaseController
    {

        protected override string navTabId
        {
            get { return "System_SysParm"; }
        }

        public ActionResult Index()
        {
            return View(GetSystemConfigList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {

           
            return View(GetSystemConfigList());
        }

        List<GSLMS.Entity.TB_SYS_PARM> GetSystemConfigList()
        {
            var searchParm =new  GSLMS.Entity.TB_SYS_PARM();
            List<GSLMS.Entity.TB_SYS_PARM> systemConfigList = serviceSysParm.GetList(searchParm);
            ViewData["RowCount"] = 0;
            return systemConfigList;
        }

        public ActionResult Create(string id)
        {
            GSLMS.Entity.TB_SYS_PARM systemConfig = new GSLMS.Entity.TB_SYS_PARM();
            if (id != null && id != "0")
            {
                systemConfig = serviceSysParm.GetByID(Helper.ToLong(id));
                if (systemConfig != null)
                {
                    ViewData["action"] = "edit";
                }
            }
            return View(systemConfig);
        }

        //
        // POST: /SystemConfig/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                GSLMS.Entity.TB_SYS_PARM systemConfig = new GSLMS.Entity.TB_SYS_PARM();
                TryUpdateModel(systemConfig, collection);

                if (systemConfig.ID>0||Request["action"] == "edit")
                {
                    serviceSysParm.Update(systemConfig);
                }
                else
                {
                    serviceSysParm.Add(systemConfig);
                }
               // HttpContext.Cache.Remove("SystemConfigAll");
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }



        //
        // GET: /SystemConfig/Delete/5
        [HttpPost]
        public string Delete(string id, string guid)
        {
            try
            {
                if (serviceSystemConfig.Delete(id))
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
